<?php

function custom_register_widgets() { /* rename this or incorporate into your own custom scheme as nec */
	global $current_blog;
	
	/* Featured Groups Widget */
	register_sidebar_widget( __('Featured Groups', 'buddypress'), 'groups_widget_groups_featured');
	register_widget_control( __('Featured Groups', 'buddypress'), 'groups_widget_groups_featured_control' );
}
add_action( 'plugins_loaded', 'custom_register_widgets' );




function groups_widget_groups_featured($args) {
	global $current_blog, $bp;
	
    extract($args);
	$options = get_option('widget_groups_featured' );
	$title = empty( $options['featured_title'] ) ? __( 'Featured Groups' ) : apply_filters('widget_title', $options['featured_title']);
	$includes = empty( $options['featured_include'] ) ? '' : $options['featured_include'];
	$includes = explode(",", $includes);

?>
	<?php echo $before_widget; ?>
	<?php echo $before_title . $title . $after_title; ?>
	
	
	
	<?php if ( $includes[0] != "" ) : ?>

	
		<ul id="featured-groups-list" class="item-list">
			<?php foreach ( $includes as $include ) : ?>
				<?php $group = new BP_Groups_Group( $include, false ); ?>
				
				<li>
					<div class="item-avatar">
						<a href="<?php echo bp_group_permalink( $group ) ?>" title="<?php echo $group->name ?>"><img src="<?php echo $group->avatar_thumb; ?>" alt="<?php echo $group->name ?> Avatar" class="avatar" /></a>
					</div>

					<div class="item">
						<div class="item-title"><a href="<?php echo bp_group_permalink( $group ) ?>" title="<?php echo $group->name ?>"><?php echo $group->name ?></a></div>
						<div class="item-meta">
						<span class="activity">
							<?php 
							if ( $group->total_member_count == 1 )
								echo $group->total_member_count . __(' member', 'buddypress');
							else
								echo $group->total_member_count . __(' members', 'buddypress');
							?>
						</span></div>
					</div>
				</li>
				<?php $counter++; ?>	
			<?php endforeach; ?>
		</ul>
		
		<?php 
		if ( function_exists('wp_nonce_field') )
			wp_nonce_field( 'widget_groups_featured', '_wpnonce-featured-groups' );
		?>
			
	<?php else: ?>
		<div class="widget-error">
			<?php _e('There are no featured groups to display.', 'buddypress') ?>
		</div>
	<?php endif; ?>
		
	<?php echo $after_widget; ?>
<?php
}

function groups_widget_groups_featured_control() {
	global $current_blog;
	
	$options = $newoptions = get_option('widget_groups_featured');
	
	if ( isset($_POST['featured-submit']) ) {
		$newoptions['featured_title'] = strip_tags(stripslashes($_POST['featured-title']));
		$newoptions['featured_include'] = strip_tags( stripslashes( $_POST['featured-include'] ) );
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option( 'widget_groups_featured', $options );
	}
	$title = attribute_escape($options['featured_title']);
	$include = attribute_escape( $options['featured_include'] );
?>
		<p><label for="featured-title"><?php _e('Title:'); ?> <input class="widefat" id="featured-title" name="featured-title" type="text" value="<?php echo $title; ?>" /></label></p>
		<p>
			<label for="featured-include"><?php _e( 'Include:'); ?> <input type="text" value="<?php echo $include; ?>" name="featured-include" id="featured-include" class="widefat" /></label>
			<br />
			<small><?php _e( 'Group IDs, separated by commas.' ); ?></small>
		</p>
		<input type="hidden" id="featured-submit" name="featured-submit" value="1" />
<?php

}











?>